#include "CompressDialog.h"
#include "ui_CompressDialog.h"
#include <QDebug>
#include <QFileDialog>
#include "PlayerWindow/AppConfig.h"
#include "thread"
#include<QFileInfo>
CompressDialog::CompressDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CompressDialog)
{
    ui->setupUi(this);
    // 去掉问号，只保留关闭
    setWindowTitle("压缩视频");
    setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
    resize(500,550);
    //将窗口永远置于上层可见 不会被遮盖
    //setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
    //阻塞除当前窗口之外的所有窗口
    //setWindowModality(Qt::ApplicationModal);
}

CompressDialog::~CompressDialog()
{
    delete ui;
}

void CompressDialog::on_buttonBox_accepted()
{


}
void CompressDialog::init(int64_t rate, int fps, int width, int height,QString fileName){
    filePath = fileName;
    qDebug()<<"比特率："<<QString::number(rate);
    ui->label_2->setText(QString::number(rate).append(" kb/s"));
   ui->label_4->setText(QString::number(fps).append(" fps"));
   ui->label_6->setText(QString::number(width));
    ui->label_8->setText(QString::number(height));
    ui->label_10->setText(fileName);
    ui->label_10->adjustSize();
    ui->checkBox->setChecked(true);
    ui->checkBox_2->setChecked(true);
    ui->lineEdit->setText(QString::number(width));
      ui->lineEdit_2->setText(QString::number(height));
      ui->horizontalSlider_2->setRange(0,51);
      ui->horizontalSlider->setRange(0,rate);
      ui->horizontalSlider_2->setValue(fps);
      ui->horizontalSlider->setValue(rate);
      ui->lineEdit_3->setText(AppConfig::videoFileOutPath);
      ui->label_18->setText(QString::number(rate));
      ui->label_19->setText(QString::number(fps));


      pd = new QProgressDialog("当前进度...", "取消", 0, 100);
      pd->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);        //不显示进度条上的“最小化”“最大化”“关闭”
      pd->setWindowTitle("视频压缩中......");    //窗口标题
      pd->setAutoClose(true);        //进度达到最大值时不关闭，默认为true
      pd->setLabelText("视频压缩中...");    //显示的文本
      pd->setRange(0, 100);                //设置进度条的极值，默认为[0,100]
      //pd->setCancelButton(NULL);            //不显示取消按钮

      //    pd->show();
      pd->hide();

}

void CompressDialog::on_pushButton_clicked()
{
    QString s = QFileDialog::getExistingDirectory(
               this, QStringLiteral("选择要存放的路径"),
                AppConfig::videoFileOutPath//初始目录
                  );
    if (!s.isEmpty())
    {

        qDebug()<<s;

        std::string path=s.toStdString();
        ui->lineEdit_3->setText(s);
        AppConfig::videoFileOutPath = s;
        AppConfig::saveConfigInfoToFile();
    }
}

void CompressDialog::on_horizontalSlider_valueChanged(int value)
{
    ui->label_20->setText(QString::number(value).append("/"));
}

void CompressDialog::on_horizontalSlider_2_valueChanged(int value)
{
    ui->label_21->setText(QString::number(value).append("/"));
}

void CompressDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    QString width = ui->lineEdit->text();
    QString height = ui->lineEdit_2->text();
     int rate = ui->horizontalSlider->value();
     int fps = ui->horizontalSlider_2->value();
    bool audio= ui->checkBox->checkState()==Qt::Checked?true:false;
    bool video= ui->checkBox_2->checkState()==Qt::Checked?true:false;
    QFileInfo fileInfo(filePath);
    QString fileName = fileInfo.fileName();
   QString currentDate= QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
    QString dstPath = ui->lineEdit_3->text().append("/out_").append(currentDate).append(fileName);
    qDebug()<<dstPath;
     Transcode transcode(filePath.toStdString(),dstPath.toStdString(),width.toInt(),height.toInt(),rate,fps,video,audio);
     transcode.setVideoCompressCallBack(this);
///创建一个线程专门用来解码视频
    std::thread([&](Transcode transcode)
    {

        transcode.doTranscode();
        pd->show();
    }, transcode).detach();
}
void CompressDialog::changeCompressProcess(const double &process){
    qDebug()<<process;
    int steps = process*100;
    pd->setValue(steps);
       //终止条件
//   if (steps >= pd->maximum()) {
//       steps = 0;
//       pd->setValue(0);

//   }


}
